package com.example.weibo.Controller;

import com.example.weibo.mapper.CommentMapper;
import com.example.weibo.pojo.dto.CommentDto;
import com.example.weibo.pojo.entity.Comment;
import com.example.weibo.pojo.vo.CommentVO;
import com.example.weibo.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Name: CommentController
 * @Author: 西风残叶
 * @Company: 朝兮网络工作室
 * @Data: 2023/11/19 21:47
 */
@RestController
public class CommentController {
    @Autowired
    CommentMapper mapper;

    /**
     * 业务:评论
     * @param comment : 评论对象
     * @param session : 会话对象判断是否登录
     */
    @RequestMapping("/comment/insert")
    public int insert(@RequestBody CommentDto comment , HttpSession session){
        // 获取会话对象
        UserVO user = (UserVO) session.getAttribute("user");
        // 对获取的会话对象判断是否登录
        if (user ==null){ // 未登录
            return 0;
        }
        System.out.println("comment = "+comment);
        // 把DTO里面的数据装进实体类
        Comment c = new Comment();
        BeanUtils.copyProperties(comment ,c);
        // 给评论设置作者的id,设置成当前登录的用户id
        c.setUserId(user.getId());
        // 调用CommentMapper里面的insert方法
        mapper.insert(c);
        return 1;
    }

    /**
     * 业务:查询所有的评论
     * @param id :微博的id
     * @return
     */
    @RequestMapping("/comment/selectByWeiboId")
    public List<CommentVO> selectByWeiboId(int id){
        return mapper.selectByWeiboId(id);
    }
}
